var UpdateOrderImg = function () {
  var template = /* html */ `
    <div  
      v-loading.fullscreen.lock="loading"
      element-loading-text="正在更新订单图片"
      style="position:absolute;top:-9999px;left:-9999px;">
      <iframe v-if="showFrame" :src="basePath+'/html/createOrder/template.html?refreshTemplateImg=1&order_no='+orderId">
      </iframe>
    </div>
  `;

  return {
    props: {
      orderId: {
        type: String,
        required: true
      }
    },

    watch: {
      orderId: function () {
        this.toOpenFrame();
      }
    },

    data: function () {
      return {
        loading: false,
        showFrame: false,
        timeout: null,
        basePath: SysUtil.getBasePath()
      };
    },

    created: function () {
      this.toOpenFrame();
    },

    mounted: function () {
      var self = this;
      $(window).on('update-complete', function (event, res) {
        self.showFrame = false;
        self.loading = false;
        var timeout = self.timeout;
        if (timeout !== null) {
          clearTimeout(timeout);
        }
        self.clearLocalItem();
        var result = res.result;
        if (!result) {
          self.$alert(res.info, '信息');
          return;
        }

        var url = res.data[0];
        self.$emit('update-success', url);
      });
    },

    beforeDestroy: function () {
      $(window).off('update-complete');
    },

    methods: {
      toOpenFrame: function () {
        var orderId = this.orderId;
        this.loading = true;
        var self = this;
        ajaxRequest('createOrder/getTemplateInfoByOrderNo', {
          order_no: orderId
        }).then(function (orderTemp) {
          if (orderTemp === null) {
            self.loading = false;
            self.$alert(`抱歉,订单编号${orderId}查找不到模版`, '信息');
            return;
          }
          var local = window.localStorage;
          local.setItem("temp_info_select", orderTemp.temp_info);
          local.setItem("temp_info_table_source_arr", orderTemp.table_source_arr);
          local.setItem("temp_info_table_source_arr_str", orderTemp.table_source_arr_str);
          local.setItem("table_source_line_total", orderTemp.table_source_line_total);
          local.setItem("template_id", orderTemp.id);
          local.setItem("temp_info_table_source_arr_for_two", orderTemp.table_source_arr_for_two);
          local.setItem("temp_info_table_source_arr_str_for_two", orderTemp.table_source_arr_str_for_two);
          local.setItem("table_source_line_total_for_two", orderTemp.table_source_line_total_for_two);
          self.showFrame = true;
          self.timeout = setTimeout(function () {
            self.updateError();
          }, 20 * 1000);
        }).catch(function () {
          self.loading = false;
        });
      },

      updateError: function () {
        this.showFrame = false;
        this.loading = false;
        this.clearLocalItem();
        var msg = '网络太慢更新订单图片失败';
        this.$alert(msg, '信息');
        this.$emit('update-error', msg)
      },

      clearLocalItem: function () {
        var local = window.localStorage;
        local.removeItem("temp_info_select");
        local.removeItem("temp_info_table_source_arr");
        local.removeItem("temp_info_table_source_arr_str");
        local.removeItem("table_source_line_total");
        local.removeItem("template_id");
        local.removeItem("temp_info_table_source_arr_for_two");
        local.removeItem("temp_info_table_source_arr_str_for_two");
        local.removeItem("table_source_line_total_for_two");
      }
    },

    template: template
  };
}();